package com.qiche.service.impl;

import com.qiche.aspect.Result;
import com.qiche.constant.AccountType;
import com.qiche.dto.CardLinkDto;
import com.qiche.mapper.*;
import com.qiche.model.*;
import com.qiche.service.CardLinkService;
import com.qiche.util.ApplicationUtils;
import com.qiche.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * @author Linzewen
 *
 *         2018年2月27日
 */
@Service("cardLinkService")
public class CardLinkServiceImpl extends BaseService<CardLink> implements CardLinkService {

	@Resource
	private CardLinkMapper mapper;

	@Resource
	private InsurancePolicyMapper policyMapper;

	@Resource
	private GiftPackageMapper giftPackageMapper;


	@Resource
	private StoreMemberMapper storeMemberMapper;

	@Override
	public CardLink selectById(Long id) {
		Example example = new Example(CardLink.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("id", id);
		List<CardLink> linkList = selectByExample(example);
		if (linkList.size() > 0) {
			return linkList.get(0);
		}
		return null;
	}

	@Override
	public List<CardLink> selectByMemberId(Long memberId) {
		Example example = new Example(CardLink.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("memberId", memberId);
		List<CardLink> links = selectByExample(example);
		return links;
	}

	@Override
	public List<CardLink> selectByPolicyId(Long policyId) {
		Example example = new Example(CardLink.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("policyId", policyId);
		List<CardLink> links = selectByExample(example);
		return links;
	}

	@Override
	public List<CardLink> selectByMemberAndPolicy(Long memberId, Long policyId) {
		Example example = new Example(CardLink.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("memberId", memberId);
		criteria.andEqualTo("policyId", policyId);
		List<CardLink> links = selectByExample(example);
		return links;
	}

	@Override
	public List<CardLink> selectByPack(Long packId) {
		Example example = new Example(CardLink.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("packId", packId);
		List<CardLink> links = selectByExample(example);
		return links;
	}

	@Override
	@Transactional
	public Result<GiftPackage> setGiftLinks(CardLinkDto cardLinkDto) {
	/*	List<CardLink> links=new ArrayList<>();
		for (CardLink cardLink : jsonObjs) {
			JSONObject.to
		}*/
		User currentUser=UserUtils.getCurrentUser();
		List<CardLink> links=cardLinkDto.getLinks();
		Long qmerchantId=null;
		if(AccountType.ADMIN==currentUser.getAccountType()){
			qmerchantId=cardLinkDto.getMerchantId();
		}else{
			qmerchantId=currentUser.getMerchantId().longValue();
		}

		Long memberId = links.get(0).getMemberId();
		Long policyId = links.get(0).getPolicyId();
		Long packId = links.get(0).getPackId();

		List<CardLink> dbLinks = null;
		List<CardLink> linkstoDelete = new ArrayList<>();
		GiftPackage pack = null;
		if (packId != 0) {
			dbLinks = selectByPack(packId);
			List<Long> LinkIds = new ArrayList<>();
			for (CardLink link : links) {
				LinkIds.add(link.getId());
			}
			for (CardLink link : dbLinks) {
				if (!LinkIds.contains(link.getId())) {
					linkstoDelete.add(link);
				}
			}
			for (CardLink cardLink : linkstoDelete) {
				mapper.deleteByPrimaryKey(cardLink.getId());
			}

			for (CardLink link : links) {
				link.setPackId(packId);
			}
			pack = giftPackageMapper.selectByPrimaryKey(packId);
		} else {
			// 新的包
			pack = new GiftPackage();
			StoreMember storeMember =storeMemberMapper.selectByPrimaryKey(memberId);

			InsurancePolicy policy = policyMapper.selectByPrimaryKey(policyId);
			pack.setCode(ApplicationUtils.getRandomString(8));
			pack.setOwnerId(memberId);
			pack.setPolicyId(policyId);
			pack.setCreateTime(new Date());
			pack.setStatus(0);
			pack.setPhone(storeMember.getPhone());

			//设置礼包门店归属
			pack.setMerchantId(qmerchantId);
			if (pack.getPolicyId() == 0) {
				/////////////// pack.setPlateNumber(member.getPlateNumber());
				pack.setPolicyCode("无");

			} else {
				pack.setPlateNumber(policy.getPlateNumber());
				pack.setPolicyCode(policy.getCode());
			}

			giftPackageMapper.insert(pack);
			for (CardLink link : links) {
				link.setPackId(pack.getId());
			}
		}

		// 新增或者更新的操作
		for (CardLink link : links) {
			if (link.getId() != null && link.getId() != 0) {
				mapper.updateByPrimaryKeySelective(link);
			} else {
				mapper.insert(link);
			}
		}

		return Result.createBySuccess("设置成功",pack);
	}
}
